﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TryTM
{
    public partial class frmMain : Form
    {//в данной части работа посвящена добавлению/удалению файликов в главный список документов

        string Path_to_file_with_Main_List; //путь, где в xml хранится список используемых документов
        DataTable DataTable_UsedFiles;
        DataSet DataSet_UsedFiles;
        string DataTable_UsedFiles_Name = "MainList";
        DataTable DataTable_WORD_FILE_POSITION;
        string DataTable_WORD_FILE_POSITION_Name = "DataTable_WORD_FILE_POSITION";
        DataTable DataTable_WORDS;
        string DataTable_WORDS_Name = "DataTable_WORDS";
        DataTable DataTable_FILES;
        string DataTable_FILES_Name = "DataTable_FILES";
        DataTable DataTable_TF_IDF;
        string DataTable_TF_IDF_Name = "DataTable_TF_IDF";
        DataTable DataTable_KeyWords;
        string DataTable_KeyWords_Name = "DataTable_KeyWords";
        DataTable DataTable_FoundAuthors;
        string DataTable_FoundAuthors_Name = "DataTable_FoundAuthors";
        DataTable DataTable_FoundUDK;
        string DataTable_FoundUDK_Name = "DataTable_FoundUDK";


        ClassUsedFileExtentions UsedFileExtentions;
        public void CreateExtentionList()
        {
            UsedFileExtentions = new ClassUsedFileExtentions();
            ClassUsedExtention CurUsedExt;
            CurUsedExt = new ClassUsedExtention(".pdf", new CreateViewOfCertainFileType_delegate(Create_View_for_PDF));
            UsedFileExtentions.AddItem(CurUsedExt);
            CurUsedExt = new ClassUsedExtention(".doc", new CreateViewOfCertainFileType_delegate(Create_View_for_Doc));
            UsedFileExtentions.AddItem(CurUsedExt);
        }

        public void Load_Data_To_Main_List() //загрузка списка используемых документов
        {
            Path_to_file_with_Main_List = Application.StartupPath + @"\Resources\MainList.hv";
            //DataTable_UsedFiles_Name = "MainList";
            //DataTable_WORD_FILE_POSITION_Name = "DataTable_WORD_FILE_POSITION";
            DataSet_UsedFiles = new DataSet();
            if (File.Exists(Path_to_file_with_Main_List))
            {//load data from file
                DataSet_UsedFiles.ReadXml(Path_to_file_with_Main_List);
                //DataSet_UsedFiles.ReadXmlSchema(Path_to_file_with_Main_List + "q");
                DataTable_UsedFiles = DataSet_UsedFiles.Tables[DataTable_UsedFiles_Name];
                //List<DataColumn> dcPKey = new List<DataColumn>();
                //dcPKey.Add(DataTable_UsedFiles.Columns[ClassMainTableColumns.FullPathToFile]);
                //DataTable_UsedFiles.PrimaryKey = dcPKey.ToArray();
                DataTable_WORD_FILE_POSITION = DataSet_UsedFiles.Tables[DataTable_WORD_FILE_POSITION_Name];
                DataTable_WORDS = DataSet_UsedFiles.Tables[DataTable_WORDS_Name];
                DataTable_FILES = DataSet_UsedFiles.Tables[DataTable_FILES_Name];
                DataTable_TF_IDF = DataSet_UsedFiles.Tables[DataTable_TF_IDF_Name];
                DataTable_KeyWords = DataSet_UsedFiles.Tables[DataTable_KeyWords_Name];
                DataTable_FoundAuthors = DataSet_UsedFiles.Tables[DataTable_FoundAuthors_Name];
                DataTable_FoundUDK = DataSet_UsedFiles.Tables[DataTable_FoundUDK_Name];
            }
            else
            {//create table and other
                DataTable_UsedFiles = Create_Table_UsedFiles(DataTable_UsedFiles_Name);
                DataSet_UsedFiles.Tables.Add(DataTable_UsedFiles);
                DataTable_WORD_FILE_POSITION = Create_Table_To_Keep_View_WORD_FILE_POSITION(DataTable_WORD_FILE_POSITION_Name);
                DataSet_UsedFiles.Tables.Add(DataTable_WORD_FILE_POSITION);
                DataTable_WORDS = Create_Table_To_Keep_Words(DataTable_WORDS_Name);
                DataSet_UsedFiles.Tables.Add(DataTable_WORDS);
                DataTable_FILES = Create_Table_To_Keep_Files(DataTable_FILES_Name);
                DataSet_UsedFiles.Tables.Add(DataTable_FILES);
                DataTable_TF_IDF = Create_Table_To_Keep_View_TF_IDF(DataTable_TF_IDF_Name);
                DataSet_UsedFiles.Tables.Add(DataTable_TF_IDF);
                DataTable_KeyWords = Create_Table_To_Keep_KeyWords(DataTable_KeyWords_Name);
                DataSet_UsedFiles.Tables.Add(DataTable_KeyWords);
                DataTable_FoundAuthors = Create_Table_To_Keep_FoundAuthors(DataTable_FoundAuthors_Name);
                DataSet_UsedFiles.Tables.Add(DataTable_FoundAuthors);
                DataTable_FoundUDK = Create_Table_To_Keep_FoundAuthors(DataTable_FoundUDK_Name);
                DataSet_UsedFiles.Tables.Add(DataTable_FoundUDK);
            }
            dataGridView_UsedFiles.DataSource = DataTable_UsedFiles;
            for (int IND = 0; IND < DataTable_UsedFiles.Columns.Count; IND++)
            {//it doesn't want to show russian column names without this hack
                dataGridView_UsedFiles.Columns[IND].HeaderText = DataTable_UsedFiles.Columns[IND].Caption;
            }
            dataGridViewFoundKeyWords.AutoGenerateColumns = true;
            Refresh_DataGridKeyWords();
            Refresh_DataGridFoundAuthors();
            Refresh_DataGridFoundUDK();
        }

        public DataTable Create_Table_To_Keep_FoundAuthors(string DataTable_UsedFiles_Name)
        {
            DataTable DataTable_UsedFiles = new DataTable(DataTable_UsedFiles_Name);
            DataColumn dc;
            List<DataColumn> dcPKey = new List<DataColumn>();
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_FoundAuthors_Columns.ID_FullPathToFile, Type.GetType("System.Int32"));
            dcPKey.Add(dc);
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_FoundAuthors_Columns.Author, Type.GetType("System.String"));
            dcPKey.Add(dc);
            DataTable_UsedFiles.PrimaryKey = dcPKey.ToArray();
            return DataTable_UsedFiles;
        }

        public DataTable Create_Table_UsedFiles(string DataTable_UsedFiles_Name)
        {
            DataTable DataTable_UsedFiles = new DataTable(DataTable_UsedFiles_Name);
            DataColumn dc;
            List<DataColumn> dcPKey = new List<DataColumn>();
            dc = DataTable_UsedFiles.Columns.Add(ClassMainTableColumns.FullPathToFile, Type.GetType("System.String"));
            dc.Caption = "Путь к файлу";
            dcPKey.Add(dc);
            dc = DataTable_UsedFiles.Columns.Add(ClassMainTableColumns.FileName, Type.GetType("System.String"));
            dc.Caption = "Имя файла";
            dc = DataTable_UsedFiles.Columns.Add(ClassMainTableColumns.FileExtention, Type.GetType("System.String"));
            dc.Caption = "Расширение файла";
            dc = DataTable_UsedFiles.Columns.Add(ClassMainTableColumns.FileAddDelete, Type.GetType("System.String"));
            dc.Caption = "Изменение файлов";
            dc = DataTable_UsedFiles.Columns.Add(ClassMainTableColumns.FileIsUsed, Type.GetType("System.String"));
            dc.Caption = "Создано представление";
            DataTable_UsedFiles.PrimaryKey = dcPKey.ToArray();
            return DataTable_UsedFiles;
        }

        public DataTable Create_Table_To_Keep_KeyWords(string DataTable_UsedFiles_Name)
        {
            DataTable DataTable_UsedFiles = new DataTable(DataTable_UsedFiles_Name);
            DataColumn dc;
            List<DataColumn> dcPKey = new List<DataColumn>();
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_KeyWords_Columns.ID_Word, Type.GetType("System.Int32"));
            dcPKey.Add(dc);
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_KeyWords_Columns.ID_FullPathToFile, Type.GetType("System.Int32"));
            dcPKey.Add(dc);
            DataTable_UsedFiles.PrimaryKey = dcPKey.ToArray();
            return DataTable_UsedFiles;
        }

        public DataTable Create_Table_To_Keep_View_TF_IDF(string DataTable_UsedFiles_Name)
        {
            DataTable DataTable_UsedFiles = new DataTable(DataTable_UsedFiles_Name);
            DataColumn dc;
            List<DataColumn> dcPKey = new List<DataColumn>();
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_TF_IDF_Columns.ID_Word, Type.GetType("System.Int32"));
            dcPKey.Add(dc);
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_TF_IDF_Columns.ID_FullPathToFile, Type.GetType("System.Int32"));
            dcPKey.Add(dc);
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_TF_IDF_Columns.Word_TF_IDF, Type.GetType("System.Double"));
            DataTable_UsedFiles.PrimaryKey = dcPKey.ToArray();
            return DataTable_UsedFiles;
        }

        public DataTable Create_Table_To_Keep_View_WORD_FILE_POSITION(string DataTable_UsedFiles_Name)
        {
            DataTable DataTable_UsedFiles = new DataTable(DataTable_UsedFiles_Name);
            DataColumn dc;
            List<DataColumn> dcPKey = new List<DataColumn>();
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_WORD_FILE_POSITION_Columns.ID_Word, Type.GetType("System.Int32"));
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_WORD_FILE_POSITION_Columns.ID_FullPathToFile, Type.GetType("System.Int32"));
            dcPKey.Add(dc);
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_WORD_FILE_POSITION_Columns.PositionInFile, Type.GetType("System.Int32"));
            dcPKey.Add(dc);
            DataTable_UsedFiles.PrimaryKey = dcPKey.ToArray();
            return DataTable_UsedFiles;
        }

        public DataTable Create_Table_To_Keep_Words(string DataTable_UsedFiles_Name)
        {
            DataTable DataTable_UsedFiles = new DataTable(DataTable_UsedFiles_Name);
            DataColumn dc;
            List<DataColumn> dcPKey = new List<DataColumn>();
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_WORDS_Columns.ID_Word, Type.GetType("System.Int32"));
            dc.AutoIncrement = true;
            dc.AutoIncrementSeed = 0;
            dc.AutoIncrementStep = 1;
            dcPKey.Add(dc);
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_WORDS_Columns.Word, Type.GetType("System.String"));
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_WORDS_Columns.Word_IDF, Type.GetType("System.Double"));
            DataTable_UsedFiles.PrimaryKey = dcPKey.ToArray();
            return DataTable_UsedFiles;
        }

        public DataTable Create_Table_To_Keep_Files(string DataTable_UsedFiles_Name)
        {
            DataTable DataTable_UsedFiles = new DataTable(DataTable_UsedFiles_Name);
            DataColumn dc;
            List<DataColumn> dcPKey = new List<DataColumn>();
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_FILES_Columns.ID_FullPathToFile, Type.GetType("System.Int32"));
            dc.AutoIncrement = true;
            dc.AutoIncrementSeed = 0;
            dc.AutoIncrementStep = 1;
            dcPKey.Add(dc);
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_FILES_Columns.FullPathToFile, Type.GetType("System.String"));
            dc = DataTable_UsedFiles.Columns.Add(Class_Table_FILES_Columns.FileLanguage, Type.GetType("System.Int32"));
            DataTable_UsedFiles.PrimaryKey = dcPKey.ToArray();
            return DataTable_UsedFiles;
        }

        public void AddRowToTable(DataTable ToWhatTable, string FileAddDelete, string FileExtention,
            string FileIsUsed, string FileName, string FullPathToFile)
        {
            DataRow CurRow;
            CurRow = ToWhatTable.NewRow();
            CurRow[ClassMainTableColumns.FileAddDelete] = FileAddDelete;
            CurRow[ClassMainTableColumns.FileExtention] = FileExtention;
            CurRow[ClassMainTableColumns.FileIsUsed] = FileIsUsed;
            CurRow[ClassMainTableColumns.FileName] = FileName;
            CurRow[ClassMainTableColumns.FullPathToFile] = FullPathToFile;
            ToWhatTable.Rows.Add(CurRow);
        }

        public void Add_Files_To_Main_List()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = Application.StartupPath;
            dlg.AddExtension = true;
            dlg.CheckFileExists = true;
            //фильтр нужно создавать согласно списку
            dlg.Filter = UsedFileExtentions.GetFilter_ForOpenFileDlg();
            dlg.Multiselect = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string[] ResultFileNames;
                string[] ResultSafeFileNames;
                ResultFileNames = dlg.FileNames;
                ResultSafeFileNames = dlg.SafeFileNames;
                DataRow[] SelRows;
                bool NoOtherFile = true;
                for (int IND = 0; IND < ResultFileNames.Count(); IND++)
                {//здесь нужно перепроверить предложенные расширения, согласно списку... 
                    if (!UsedFileExtentions.HasSuchExtention(System.IO.Path.GetExtension(ResultSafeFileNames[IND])))
                    {
                        NoOtherFile = false;
                    }
                    else
                    {
                        //проверить, есть ли уже такой файлик в таблице
                        SelRows = (from DataRow mi in DataTable_UsedFiles.Rows
                                   where mi[ClassMainTableColumns.FullPathToFile].ToString().Trim().ToLower() == ResultFileNames[IND].Trim().ToLower()
                                   select mi).ToArray();
                        if (SelRows.Count() > 0)
                        {//такое чудо уже есть в таблице
                            SelRows[0][ClassMainTableColumns.FileAddDelete] = ClassAddDeleteNoChange.FileToAdd;
                        }
                        else
                        {//такого чуда ещё нет в таблице
                            //добавить в таблицу
                            AddRowToTable(DataTable_UsedFiles, ClassAddDeleteNoChange.FileToAdd, System.IO.Path.GetExtension(ResultSafeFileNames[IND]).ToLower().Trim(),
                                ClassUsedOrNotUsed.FileWasNotUsed, ResultSafeFileNames[IND], ResultFileNames[IND]);
                        }
                    }
                    dataGridView_UsedFiles.Refresh();
                    if (!NoOtherFile)
                    {
                        MessageBox.Show("Была попытка добаить файл левого расширения. Попытка не удалась.");
                    }
                }
            }
        }

        public void Check_As_Deleted_In_Main_List()
        {//отметить выделенные строки на удаление, удаление из списка производить, когда начинает строиться представление
            string Path_CurDGVRow;
            DataRow CurRow;
            for (int IND = 0; IND < dataGridView_UsedFiles.SelectedRows.Count; IND++)
            {
                Path_CurDGVRow = dataGridView_UsedFiles.SelectedRows[IND].Cells[ClassMainTableColumns.FullPathToFile].Value.ToString();
                CurRow = DataTable_UsedFiles.Rows.Find(Path_CurDGVRow);
                CurRow[ClassMainTableColumns.FileAddDelete] = ClassAddDeleteNoChange.FileToDelete;
            }
            dataGridView_UsedFiles.Refresh();
        }
    }
}
